﻿/// <reference path="../_ref.js" />

//Hack around to get Bootstrap 2.1.1 dropdown the work :(
$('body').on('touchstart.dropdown', '.dropdown-menu', function (e) { e.stopPropagation(); });

//Custom prototypes
String.prototype.startsWith = function (str) {
    return this.match("^" + str);
};

String.prototype.endsWith = function (str) {
    return this.match(str + "$");
};

String.prototype.contains = function (str) {
    return this.indexOf(str) != -1;
};

//My jQuery plugins
(function ($) {

    $.fn.exists = function (selector) {
        return this.length > 0;
    };

    $.isValidEmail = function (email) {
        var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        return regex.test(email);
    };

    $.isValidUrl = function (url) {
        //var regex = /^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/;
        var regex = /^(https?:\/\/)/;
        return regex.test(url);
    }
    $.isAppleDevice = function () {
        return (
            navigator.userAgent.toLowerCase().contains("ipad") ||
            navigator.userAgent.toLowerCase().contains("iphone") ||
            navigator.userAgent.toLowerCase().contains("ipod")
        );
    }

    $.isBigFile = function (file) {
        return file.size > maxRequestSize;
    }
})(jQuery);

//Global setup
$(document).ready(function () {
    //Enable loading button
    $('.btn[data-loading-text]').click(function () {
        if ($('form').valid())
            $(this).button('loading');
    });
});

/////////////////// Helpers /////////////////////

//Dynamically attach / detach an <input type=file /> to the DOM
var novoFileUploadId = "fileUploadNovo";
var novoFileUpload = undefined;
function attachFileUpload(attach) {
    //Precheck
    novoFileUpload = $('#' + novoFileUploadId);
    //Attach
    if (attach && !novoFileUpload.exists()) {
        //Markup
        $('body').append('<input id="' + novoFileUploadId + '" ' + 'name="' + novoFileUploadId + '" class="novo-file-upload" type="file" />');
        //Event handler
        novoFileUpload = $('#' + novoFileUploadId);
        novoFileUpload.change(function () {
            novoFileUpload = $('#' + novoFileUploadId);
            novoUpload();
        });
    }

    //Detach
    if (!attach && novoFileUpload.exists()) {
        novoFileUpload.detach();
    }
}